/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.sys.db.util;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.govProcure.bean.BuildAssetBean;
import com.yunhoo.sys.login.LoginBean;
import com.yunhoo.sys.util.common.LoginParam;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

/**
 *  链接远程数据库
 * @author Administrator
 */
public class ConnectRemoteDB {

    private String SERVER = ""; //远程服务器
    private String DB = ""; //远程数据库
    private String UID = ""; //用户名
    private String PWD = ""; //密码

    public ConnectRemoteDB(String configType) {
        try {
//            ServletContext scontext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
//            Properties props = new Properties();
//            InputStream in = new FileInputStream(scontext.getRealPath(filePath));
//            props.load(in);
            Map<String, String> props = initMap(configType);
            SERVER = props.get("dbServer");
            DB = props.get("dbName");
            UID = props.get("uid");
            PWD = props.get("pwd");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Map<String, String> initMap(String db_type) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        Map<String, String> map = null;
        try {
            client.startTransaction();
            
            Map paramMap = new HashMap();
            paramMap.put("orgCode",LoginParam.getOrg().getCode());
            paramMap.put("config_type",db_type);
            map = client.queryForMap("sysConfig.queryMapConfig", paramMap, "key", "value");
            
            client.commitTransaction();
        } catch (SQLException ex) {
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(LoginBean.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(BuildAssetBean.class.getName()).log(Level.SEVERE, null, ex);
        }
        return map;
    }
    public String getDB() {
        return DB;
    }

    public void setDB(String DB) {
        this.DB = DB;
    }

    public String getPWD() {
        return PWD;
    }

    public void setPWD(String PWD) {
        this.PWD = PWD;
    }

    public String getSERVER() {
        return SERVER;
    }

    public void setSERVER(String SERVER) {
        this.SERVER = SERVER;
    }

    public String getUID() {
        return UID;
    }

    public void setUID(String UID) {
        this.UID = UID;
    }
}
